package com.niit.pys.controller;


import com.niit.pys.bean.Book;
import com.niit.pys.bean.Comm;
import com.niit.pys.bean.User;
import com.niit.pys.service.BookService;
import com.niit.pys.service.CommService;
import com.niit.pys.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Base64;
import java.util.List;

@Controller
public class testcontro {
    @Autowired
    private UserService userService;
    @Autowired
    private CommService commService;
    @Autowired
    private BookService bookService;
//user相关的功能是最初测试用的,数据库是spring而非该项目用的book3
    @GetMapping("/findAllUser")
    public String findAll(Model model){
        List<User> AllUser = userService.findAllUsers();
        model.addAttribute("users", AllUser);
        return "test";
    }
    @GetMapping("/findAllBooks")
    public String findAllBook(Model model){
        List<Book> books = bookService.findAllBooks();
        for (Book book : books) {
            byte[] bookCover = book.getBook_cover();
            if (bookCover != null) { // 添加对 bookCover 是否为空的检测
                String bookCoverJpg = "data:image/jpeg;base64," + Base64.getEncoder().encodeToString(bookCover);
                book.setBookCoverJpg(bookCoverJpg);
            }
        }
        model.addAttribute("books", books);
        return "Test_Book";
    }
    @GetMapping("/bookDetail")
    public String bookDetail(@RequestParam(name = "book_id") String book_id,Model model){
       Book book = bookService.findBookByid(book_id);
        model.addAttribute("book", book);
        return "bookDetail";
    }
    @GetMapping("/findAllBookComs")
    public String findAllBookCom(Model model){
        List<Comm> comms = commService.findAllComms();
        model.addAttribute("comms", comms);
        return "Test_Comm";
    }
    @GetMapping("/findUserByName")
    public String findUserByName(@RequestParam(name = "userName") String userName, Model model){
        List<User> AllUser = userService.findUserByName(userName);
        model.addAttribute("users", AllUser);
        return "test";
    }
    @GetMapping("/goToHome")
    public String goToHome() {
        return "Home";
    }
    @GetMapping("/goToAdd")
    public String goToAddUser() {
            return "add";
    }
    @GetMapping("/goToPig")
    public String goToPig() {
        return "testImag";
    }
    @GetMapping("/goToFindUserByName")
    public String goToFindUserByName() {
        return "findUserByName";
    }
    @GetMapping("/goToAddBook")
    public String addBook() {
        return "testAddCover";
    }
    @GetMapping("/goHome")
    public String home() {
        return "test";
    }
    @PostMapping("/add")
    public String addUser(@ModelAttribute User user) {
        userService.addUser(user);
        return "add";
    }
    @PostMapping("/addBookold")
    public String addBookold(@ModelAttribute("book") Book book, Model model) {
        Book newBook = new Book();
        // 将前端对象中的属性复制到新对象中
        BeanUtils.copyProperties(book, newBook);
        // 手动设置其他属性
        newBook.setUpdate_date(LocalDateTime.now());
        newBook.setCreate_date(LocalDateTime.now());
        newBook.setVersion(1);
        bookService.addBook(newBook);
        model.addAttribute("book", newBook);
        return "Test_addBook";
    }
    @PostMapping("/addBook")
    public String addBook(@ModelAttribute("book") Book book, Model model, @RequestParam("book_cover_input") MultipartFile book_cover) {

        byte[] bytes = new byte[0];
        try {
            bytes = book_cover.getBytes();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        book.setBook_cover(bytes);
        book.setUpdate_date(LocalDateTime.now());
        book.setCreate_date(LocalDateTime.now());
        book.setVersion(1);
        bookService.addBook(book);
        model.addAttribute("book", book);
        return "testAddCover";
    }
    @PostMapping("/addComm")
    public String addComm(@ModelAttribute("comm") Comm comm, Model model) {
        Comm newComm = new Comm();
//        // 将前端对象中的属性复制到新对象中
       //BeanUtils.copyProperties(book, newBook);
        BeanUtils.copyProperties(comm, newComm);
        // 手动设置其他属性

//        newBook.setUpdate_date(LocalDateTime.now());
//        newBook.setCreate_date(LocalDateTime.now());
//        newBook.setVersion(1);
//
//      bookService.addBook(newBook);
        commService.addComm(newComm);
        List<Comm> comms = commService.findAllComms();
        model.addAttribute("comms", comms);
        return "Test_Comm";
    }


    @PostMapping("/delete")
    public String deleteUser(@RequestParam("uid") int uid) {
        userService.deleteUserById(uid);
        return "add";
    }
    @PostMapping("/deleteBookById")
    public String deleteBook(@RequestParam("book_id") int book_id,Model model) {
        bookService.deleteBookById(book_id);
        return "deleteBookDown";
    }
    @PostMapping("/deleteCommById")
    public String deleteComm(@RequestParam("comm_id") int comm_id,Model model) {
        commService.deleteCommById(comm_id);
        return "deleteCommDown";
    }
    @GetMapping("/hello")
    public String hello(){
        return "hello";
    }
}
